/*
 * @Author: lirl
 * @Date: 2022-07-18 10:15:52
 * @Description: file content
 */

import { Loading } from 'element-ui';
import DOMPurify from 'dompurify';
import dayjs from 'dayjs';
import isSameOrAfter from 'dayjs/plugin/isSameOrAfter';
import isSameOrBefore from 'dayjs/plugin/isSameOrBefore';
import { isEmpty, size, toArray, entries } from 'lodash-es';
import { COMPANY_LEADER_IDENTITY_CODES, RESEARCH_REPORT_LEAST_NUMBER } from '@/utils/constants';
import { getStorageInfo } from '@/api/utils/storage-util';

// 启用插件
dayjs.extend(isSameOrAfter);
dayjs.extend(isSameOrBefore);

/**
 * 是否开发环境
 */
const isDevEnv = () => ['dev', 'test'].includes(process.env.NODE_ENV);

/**
 * 是否为生产环境
 * @returns
 */
const isProdEnv = () => process.env.NODE_ENV === 'prod';

const showLoading = (text) => Loading.service({
  lock: true,
  text,
  spinner: 'el-icon-loading',
  background: 'rgba(0, 0, 0, 0.7)',
});

/*
 * 清理 HTML 内容
 * @param {string} html - 原始 HTML 内容
 * @returns {string} - 清理后的 HTML 内容
 */
const cleanHtml = (html) => DOMPurify.sanitize(html);

/**
 * 答题确认交接提示
 * @param {number} answerScore - 答题分数
 */
const answerConfirmSubmitTip = (answerScore) => {
  const common = `交卷成功，您本次答题成绩为 ${answerScore}分。`;
  // 答题确认满分提示
  const FULL_SCORE = `${common}已达到合格标准。`;
  // 答题确认未满分提示
  const NOT_FULL_MARKS = `${common}烦请重新作答。`;

  return {
    FULL_SCORE,
    NOT_FULL_MARKS,
  }
};

/**
 * 日期选择区间
 * @param {Array} timePeriod - 时间范围
 * @param {number} selectedTime - 选中的时间
 * @returns {boolean} - 是否在范围内 true 在范围内; false 不在范围内
 */
const dateSelectionRange = (timePeriod, selectedTime) => {
  // 时间范围未设置或者未选择时间 则默认返回 true
  if(isEmpty(timePeriod) || !selectedTime) {
    return true;
  }
  const [startDate, endDate] = timePeriod;
  // 将检查的日期转换为dayjs对象
  const checkDate = dayjs(selectedTime);
  // 判断日期是否在范围内(包含边界)
  return checkDate.isSameOrAfter(startDate) && checkDate.isSameOrBefore(endDate);
};

/**
 * 类型字段将数组拼接成字符串
 */
const joinTypeField = (options, value, $set) => {
  const typeMap = new Map(options.map(({ value: optionValue, label: optionLabel }) => [optionValue, optionLabel]));
  // 找到对应的中文label
  const selectedLabels = value.map((val) => typeMap.get(val) || '');
  // 同步更新到行数据中 用,拼接
  $set('normativeTypeId', value.join(','));
  $set('normativeType', selectedLabels.join(','));
  $set('showNormativeType', selectedLabels.join('<br/>'));
};

/**
 * 把日期范围值赋值给表单字段
 * startField: 开始时间字段
 * endField: 结束时间字段
 * rangeDate: 日期范围字段
 */
const changeDateRangeValue = (value, $set, field) => {
  if (isEmpty(value)) {
    return;
  }
  // 开始时间及结束时间赋值给表单字段
  const [startDate, endDate] = value;
  const { startField, endField, rangeDate = 'rangeDate' } = field;
  $set(startField, startDate);
  $set(endField, endDate);
  $set(rangeDate, `${startDate} 至 ${endDate}`);
};

/**
 * 对表单字数进行验证
 * @param {string} value - 输入的值
 * @param {number} minimumCount - 字数限制 默认为 100
 */
const validateWordCount = (value, minimumCount = RESEARCH_REPORT_LEAST_NUMBER) => {
  // 获取表单输入字符的长度 如果输入的字数小于最小值 则返回 true: 提示校验; false: 不提示校验
  return size(toArray(value)) < minimumCount;
}

/**
 * 根据身份code判断是否是领导
 */
const isLeader = (allowedRoles, identityCode) => allowedRoles.includes(identityCode);

/**
 * 判断当前身份是否是公司级领导(正副职)
 */
const isCompanyLeader = () => isLeader(COMPANY_LEADER_IDENTITY_CODES, getStorageInfo('identityDefCode'));

export {
  isProdEnv,
  isDevEnv,
  showLoading,
  cleanHtml,
  answerConfirmSubmitTip,
  dateSelectionRange,
  joinTypeField,
  changeDateRangeValue,
  validateWordCount,
  isLeader,
  isCompanyLeader,
};
